home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / panelMethods.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  15.4 KB  |  631 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include "gl.h"
  22. #include "geom.h"
  23. #include "class.h"
  24. #include "selectors.h"
  25. #include "classIds.h"
  26. #include "colors.h"
  27. #include "mbox.h"
  28. #include "individual.h"
  29. #include "camera.h"
  30. #include "panel.h"
  31. #include "valuator.h"
  32. #include "menu.h"
  33. #include "pick.h"
  34. #include "objIds.h"
  35.  
  36. extern masterPanel mPanelTemplate;
  37. extern masterPanel *thePanel;
  38. extern panel        panelTemplate;
  39. extern colorPanel  colorPanelTemplate;
  40. extern point2d     curWindow;
  41. extern point2df    scalePanel;
  42. extern int unsophistication;
  43.  
  44. extern panel *makeValuator(), *makeValuatorf(), *makeValDelta(), *makeMenu();
  45. extern panel *makeValHue(), *makeValVal();
  46.  
  47.     /*
  48.      *  initialize a panel
  49.      */
  50.      panel *
  51. makePanel(list, dad, ScrArea, paintObj)
  52.     register panel *list, *dad;
  53.     register rectangle *ScrArea;
  54.     Object paintObj;
  55. {
  56.     register panel *aPanel;
  57.  
  58.     if ((aPanel = (panel *)clone(&panelTemplate)) == NULL)
  59.     return list;
  60.  
  61.     aPanel->next = list;
  62.     aPanel->kids = NULL;
  63.     aPanel->dad  = dad;
  64.     aPanel->area = *ScrArea;
  65.     aPanel->painter = paintObj;
  66.  
  67.     return aPanel;
  68. }
  69.  
  70.     /*
  71.      *  initialize a color panel
  72.      */
  73.      panel *
  74. makeColorPanel(list, dad, ScrArea, clientColor)
  75.     register panel *list, *dad;
  76.     register rectangle *ScrArea;
  77.     int clientColor;
  78. {
  79.     register colorPanel *aPanel;
  80.     RGBvalue r, g, b;
  81.  
  82.     if ((aPanel = (colorPanel *)clone(&colorPanelTemplate)) == NULL)
  83.     return list;
  84.  
  85.     aPanel->base.next = list;
  86.     aPanel->base.kids = NULL;
  87.     aPanel->base.dad  = dad;
  88.     aPanel->base.area = *ScrArea;
  89.     aPanel->base.painter = NULL;
  90.  
  91.     gmcolor(clientColor, &r, &g, &b);
  92.  
  93.     /* convert from rgb to hsv and stuff it */
  94.  
  95.     aPanel->hue        = 0;
  96.     aPanel->satur    = 0;
  97.     aPanel->value    = 0;
  98.     aPanel->clientColor = clientColor;
  99.  
  100.     return (panel *)aPanel;
  101. }
  102.  
  103. emptyPanel(p)
  104.     register panel *p;
  105. {
  106.     freePanel(p->next);    /* discard old dependant panels */
  107.     freePanel(p->kids);
  108.     p->next = NULL;
  109.     p->kids = NULL;
  110.  
  111.     if (p->painter == NULL) {
  112.     buildFloor(&p->area, MASTERFLOOR);
  113.     p->painter = (Object)MASTERFLOOR;
  114.     }
  115.     ((masterPanel *)p)->highwater.x = 0;
  116.     ((masterPanel *)p)->highwater.y = 0;
  117. }
  118.  
  119.     /*
  120.      *  re-initialize the master control panel's subtree of panels
  121.      */
  122. initPanel(p)
  123.     register panel *p;
  124. {
  125.     int flags;
  126.     rectangle  r, rw, rl;
  127.     point2d inset, button;
  128.     extern camera *theCamera;
  129.     extern panel *makeValuator(), *makeValDelta(), *makeMenu();
  130.  
  131.     freePanel(p->next);    /* discard old dependant panels */
  132.     freePanel(p->kids);
  133.     p->next = NULL;
  134.     p->kids = NULL;
  135.  
  136.     if (p->painter == NULL) {
  137.     buildFloor(&p->area, MASTERFLOOR);
  138.     p->painter = (Object)MASTERFLOOR;
  139.     }
  140.  
  141.     ((masterPanel *)p)->highwater.x = 255;
  142.     ((masterPanel *)p)->highwater.y = 205;
  143.  
  144.         /* leftright/updown camera control */
  145.     flags = CLIPOUT;
  146.     setrect(&r, 10,20, 150,150);
  147.     setpt2d(&inset, 0,0);
  148.     setrect(&rw, -60,-30, 120,60);
  149.     setrect(&rl, -5400,0, 5400,1800);
  150.     buildCam1(&r, CAM1);
  151.     p->kids = makeValDelta(p->kids, p, flags, &r, &inset, &rw,
  152.                 &theCamera->me.rotation.z,
  153.                 &theCamera->me.rotation.y, &rl, CAM1);
  154.     
  155.  
  156.         /* zoom camera control */
  157.     flags = CLIPOUT;
  158.     setrect(&r, 170,20, 75,150);
  159.     setpt2d(&inset, 0,0);
  160.     setrect(&rw, 0,-15, 0,30);
  161.     setrect(&rl, 0,200, 0,1700);
  162.     buildCam2(&r, CAM2);
  163.     p->kids = makeValDelta(p->kids, p, flags, &r, &inset, &rw,
  164.                  NULL,
  165.                 &theCamera->zoom, &rl, CAM2);
  166.  
  167.         /* reset camera control */
  168.     flags = NULL;
  169.     setrect(&r, 220,20, 30,30);
  170.     setpt2d(&button, 1, 1);
  171.     buildCam3(&r, CAM3, CAM3H);
  172.     p->kids = makeMenu(p->kids, p, flags, &r,&button,theCamera,HOME,CAM3,CAM3H);
  173.  
  174.         /* sophistication control */
  175.     flags = NULL;
  176.     setrect(&r, 10,180, 240,25);
  177.     setpt2d(&inset, 6,0);
  178.     setrect(&rw, 0,0, 100,0);
  179.     buildSoph(&r, SOPH, SOPHH);
  180.     p->kids = makeValuator(p->kids, p, flags, &r, &inset, &rw,
  181.                     &unsophistication,
  182.                     NULL, SOPH, SOPHH);
  183. }
  184.  
  185.     /*
  186.      *  refresh the control panel, updating it as necessary
  187.      */
  188. drawControls(aPanel)
  189.     masterPanel *aPanel;
  190. {
  191.     static inited = FALSE;
  192.  
  193.     if (! inited) {
  194.     Msg(aPanel, INIT, NOARG, NULL);
  195.     inited = TRUE;
  196.     }
  197.  
  198.     makeobj((Object)aPanel);
  199.     initnames();
  200.     loadname(0);
  201.     pushname(0);
  202.     pushLongName(aPanel);
  203.  
  204.     Msg(aPanel, DRAW, NOARG, NULL);
  205.  
  206.     popLongName();
  207.     closeobj();
  208. }
  209.  
  210.  
  211.     /*
  212.      *  draw the panel tree, beginning with the firstPanel
  213.      */
  214. drawPanel(firstPanel)
  215.     register panel *firstPanel;
  216. {
  217.     register panel *p;
  218.  
  219.     for (p = firstPanel; p; p = p->next) {
  220.     pushmatrix();
  221.     pushLongName(p);
  222.     translate((float)p->area.orig.x, (float)p->area.orig.y, 0.0);
  223.     fishWritemask(FOREGROUND);
  224.     callobj(p->painter);
  225.     Msg(p, DRAWTRACKER, NOARG, NULL);
  226.     popLongName();
  227.  
  228.     /* printf("drawPanel: 0x%x (class %d) about to draw kids 0x%x\n",
  229.                    p, p->myClass->classId, p->kids);   /* */
  230.     drawPanel(p->kids);
  231.     popmatrix();
  232.     }
  233. }
  234.  
  235.     /*
  236.      *  initialize a color-editing panel with two interconnected
  237.      *  valuators, with feedback as to the current color
  238.      */
  239. initColorVals(p, flags, scrArea, clientColor)
  240.     register panel *p;
  241.     int flags;
  242.     register rectangle *scrArea;
  243.     int clientColor;
  244. {
  245.     rectangle area;
  246.     point2d inset;
  247.     panel *dad;
  248.     static nextCid = COLORVAL1;
  249.  
  250.     setpt2d(&inset, 0,0);
  251.     buildCol(scrArea, color, nextCid, nextCid+1, nextCid+2);
  252.     dad = p->kids = makeColorPanel(p->kids, p, scrArea, clientColor);
  253.     p = dad;
  254.     area.orig.x = 0;
  255.     area.orig.y = 0;
  256.     area.extent.x = (3*(scrArea->extent.x-10))/4;
  257.     area.extent.y = scrArea->extent.y;
  258.     p->kids = makeValHue(p->kids, p, flags, &area, &inset, color,
  259.                   nextCid, nextCid+2);
  260.     area.orig.x = area.extent.x + 10;
  261.     area.extent.x = (scrArea->extent.x-10)/4;
  262.     p->kids = makeValVal(p->kids, p, flags, scrArea, &inset, color,
  263.                   nextCid+1, nextCid+2);
  264.  
  265.     /* bump parent's area */
  266.     dad->area.extent.x =
  267.         max(p->area.extent.x, scrArea->orig.x+scrArea->extent.x);
  268.     dad->area.extent.y =
  269.         max(p->area.extent.y, scrArea->orig.y+scrArea->extent.y);
  270.  
  271.     nextCid += 3;
  272. }
  273.  
  274.     /*
  275.      *  initialize a plain vanilla absolute valuator consisting of a box
  276.      *  containing a sliding bar, and a label below it.
  277.      */
  278. initVanillaVal(p, fr, to, curval, label)
  279.     panel *p;
  280.     int fr, to;        /* range of values */
  281.     int *curval;    /* initial value */
  282.     char *label;
  283. {
  284.     int flags;
  285.     rectangle scrArea, worldArea;
  286.     point2d inset;
  287.  
  288.     flags = NULL;
  289.     setrect(&scrArea, 0, getheight(), 50, 67);
  290.     setpt2d(&inset, 0,2);
  291.     if (p->kids) {
  292.     scrArea.orig.x = p->kids->area.orig.x + p->kids->area.extent.x + 10;
  293.     }
  294.     setrect(&worldArea, 0, to, 0, fr - to);
  295.     bldVanillaVal(&scrArea, label, curval, label);
  296.     p->kids = makeValuator(p->kids, p, flags, &scrArea, &inset, &worldArea,
  297.                     NULL, curval, label, curval);
  298.     /* bump parent's area */
  299.     p->area.extent.x = max(p->area.extent.x, scrArea.orig.x+scrArea.extent.x);
  300.     p->area.extent.y = max(p->area.extent.y, scrArea.orig.y+scrArea.extent.y);
  301. }
  302.  
  303.     /*
  304.      *  initialize a plain vanilla absolute floating point valuator
  305.      *  consisting of a box containing a sliding bar, and a label below it.
  306.      */
  307. initVanillaValf(p, fr, to, curval, label)
  308.     panel *p;
  309.     float fr, to;    /* range of values */
  310.     float *curval;    /* initial value */
  311.     char *label;
  312. {
  313.     int flags;
  314.     rectangle scrArea;
  315.     rectanglef worldArea;
  316.     point2d inset;
  317.  
  318.     flags = NULL;
  319.     setrect(&scrArea, 0, getheight(), 50, 67);
  320.     setpt2d(&inset, 0,2);
  321.     if (p->kids) {
  322.     scrArea.orig.x = p->kids->area.orig.x + p->kids->area.extent.x + 10;
  323.     }
  324.     setrect(&worldArea, 0.0, to, 0.0, fr - to);
  325.     bldVanillaVal(&scrArea, label, curval, label);
  326.     p->kids = makeValuatorf(p->kids, p, flags, &scrArea, &inset, &worldArea,
  327.                      NULL, curval, label, curval);
  328. }
  329.  
  330.  
  331.     /*
  332.      *  gffree the panel tree, beginning with the firstPanel
  333.      */
  334. freePanel(firstPanel)
  335.     register panel *firstPanel;
  336. {
  337.     register panel *p, *nextp;
  338.  
  339.     for (p = firstPanel; p; p = nextp) {
  340.     nextp = p->next;
  341.     /*
  342.     delobj(p->painter);
  343.     switch(p->them.me.myClass->classId) {
  344.         case VALUATOR:
  345.         delobj(((valuator *)p)->tracker);
  346.         break;
  347.         case VALUATORF:
  348.         delobj(((valuatorf *)p)->tracker);
  349.         break;
  350.         case VALMESG:
  351.         delobj(((valmesg *)p)->tracker);
  352.         break;
  353.         case VALMESGF:
  354.         delobj(((valmesgf *)p)->tracker);
  355.         break;
  356.         case MENU:
  357.         delobj(((menu *)p)->highlight);
  358.         break;
  359.         case CHOICE:
  360.         delobj(((choice *)p)->highlight);
  361.         break;
  362.         case CHOICEF:
  363.         delobj(((choicef *)p)->highlight);
  364.         break;
  365.         default:
  366.         break;
  367.     }
  368.     */
  369.     freePanel(p->kids);
  370.     dontdo(p);
  371.     gffree(p);
  372.     }
  373. }
  374.  
  375. panel *curPanel = NULL;
  376.  
  377.     /*
  378.      *  begin picking on a panel (button goes down on it)
  379.      */
  380. bselPanel(self, argtype, hit)
  381.     register panel *self;
  382.     long argtype;
  383.     hitstruct *hit;
  384. {
  385.     /* printf("bselPanel:    BEGIN SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  386.         self, ((inst *)self)->myClass->classId, curPanel); /* */
  387.  
  388.     curPanel = self;    /* hit processing all goes to pen-goes-down panel */
  389.     convertHit(curPanel, hit);
  390.     Msg(curPanel, SELECT, argtype, hit);
  391. }
  392.  
  393.     /*
  394.      *  begin picking on a new panel (button wanders into it while down)
  395.      */
  396. nselPanel(self, argtype, hit)
  397.     register panel *self;
  398.     long argtype;
  399.     hitstruct *hit;
  400. {
  401.     /* printf("nselPanel:      NEW SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  402.         self, ((inst *)self)->myClass->classId, curPanel); /* */
  403. }
  404.  
  405.  
  406.     /*
  407.      *  continue picking on a panel (button remains down on it)
  408.      */
  409. cselPanel(self, argtype, hit)
  410.     register panel *self;
  411.     long argtype;
  412.     hitstruct *hit;
  413. {
  414.     /* printf("cselPanel: CONTINUE SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  415.         self, ((inst *)self)->myClass->classId, curPanel); /* */
  416.  
  417.     if (curPanel) {
  418.     convertHit(curPanel, hit);
  419.     Msg(curPanel, SELECT, argtype, hit);
  420.     }
  421. }
  422.  
  423.     /*
  424.      *  end picking on a panel (button goes up on it)
  425.      */
  426. eselPanel(self, argtype, hit)
  427.     register panel *self;
  428.     long argtype;
  429.     hitstruct *hit;
  430. {
  431.     /* printf("eselPanel:      END SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  432.         self, ((inst *)self)->myClass->classId, curPanel); /* */
  433.  
  434.     if (curPanel) {
  435.     if (self != curPanel) {
  436.         Msg(curPanel, ENDSELECT, argtype, hit);
  437.     }
  438.     else {
  439.         convertHit(curPanel, hit);
  440.         Msg(curPanel, SELECT, argtype, hit);
  441.         Msg(curPanel, RESET, argtype, hit);
  442.         curPanel = NULL;
  443.     }
  444.     }
  445. }
  446.  
  447.     /*
  448.      *  abort picking on a panel (pen-down goes out of bounds)
  449.      */
  450. kselPanel(self, argtype, hit)
  451.     register panel *self;
  452.     long argtype;
  453.     hitstruct *hit;
  454. {
  455.     /* printf("kselPanel:      KILL SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  456.         self, ((inst *)self)->myClass->classId, curPanel); /* */
  457. }
  458.  
  459.     /*
  460.      *  no longer picking on a panel (button leaves it while down)
  461.      */
  462. oselPanel(self, argtype, hit)
  463.     register panel *self;
  464.     long argtype;
  465.     register hitstruct *hit;
  466. {
  467.     /* printf("oselPanel:      OLD SELECT on 0x%x, class %d,  curPanel 0x%x\n",
  468.             self, ((inst *)self)->myClass->classId, curPanel); /* */
  469.  
  470.     /*
  471.      * if passing out of legal control panel area, send kill select
  472.      */
  473.     if (checkForKillPanel(self, argtype, hit)) {
  474.     if (curPanel) {
  475.         Msg(curPanel, KILLSELECT, argtype, hit);
  476.         curPanel = NULL;
  477.     }
  478.     }
  479.     else {
  480.         /*
  481.          * otherwise, handle it like it was just any other continue select
  482.          */
  483.     Msg(self, CONTSELECT, argtype, hit);
  484.     }
  485.  
  486. }
  487.  
  488.     /*
  489.      * kill current panel-selection process if picking passes out of
  490.      *  control panel's viewport
  491.      */
  492. checkForKillPanel(self, argtype, hit)
  493.     register panel *self;
  494.     long argtype;
  495.     register hitstruct *hit;
  496. {
  497.     hitstruct dummy;
  498.  
  499.     dummy = *hit;
  500.  
  501.     convertHit(NULL, &dummy);    /* convert hit into viewport-relative coords */
  502.     if (dummy.x <=0) {        /* if pen-down passes into other viewport    */
  503.     return TRUE;        /* invalidate further pen-downs in panels    */
  504.     }                   /* until next bselPanel                      */
  505.     else return FALSE;
  506. }
  507.  
  508.     /*
  509.      *  process a selection in a panel
  510.      */
  511. selectPanel(self, argtype, hit)
  512.     register panel *self;
  513.     long argtype;
  514.     register hitstruct *hit;
  515. {
  516.     /* printf("selectPanel: selected panel 0x%x, class %d,  curPanel 0x%x\n",
  517.             self, ((inst *)self)->myClass->classId, curPanel); /* */
  518.  
  519.     /* printf("selectPanel: ignoring hit at %d, %d\n", hit->x, hit->y);
  520.     /* */
  521. }
  522.  
  523.     /*
  524.      *  process a deselection in a panel
  525.      */
  526. deselectPanel(self)
  527.     register panel *self;
  528. {
  529.     /* printf("deselectPanel: deselect panel 0x%x, class %d,  curPanel 0x%x\n",
  530.             self, ((inst *)self)->myClass->classId, curPanel); /* */
  531. }
  532.  
  533.     /*
  534.      *  convert hit into Panel's local coordinate space
  535.      */
  536. convertHit(aPanel, hit)
  537.     register panel *aPanel;
  538.     register hitstruct *hit;
  539. {
  540.         /* convert screen coordinates into viewport-relative coords */
  541.     hit->x = (hit->x - curWindow.y) * scalePanel.x;
  542.     hit->y = (curWindow.y - hit->y) * scalePanel.y;
  543.     /*
  544.     printf("convertHit: viewport hit = %d, %d\n", hit->x, hit->y);
  545.     /* */
  546.  
  547.         /* convert viewport coords into panel-relative coords */
  548.     while(aPanel) {
  549.     hit->x -= aPanel->area.orig.x;
  550.     hit->y -= aPanel->area.orig.y;
  551.             /*
  552.     printf("\t in panel loop, orig = %d, %d\n",
  553.            aPanel->area.orig.x, aPanel->area.orig.y);
  554.             /* */
  555.     aPanel = aPanel->dad;
  556.     }
  557.     /*
  558.     printf("convertHit: panel hit = %d, %d\n", hit->x, hit->y);
  559.     /* */
  560. }
  561.  
  562.     /*
  563.      *  convert a point in Panel's local coordinate space into viewport coords
  564.      */
  565. globalPoint(aPanel, aPoint)
  566.     register panel *aPanel;
  567.     register point2d *aPoint;
  568. {
  569.     while(aPanel) {
  570.     aPoint->x += aPanel->area.orig.x;
  571.     aPoint->y += aPanel->area.orig.y;
  572.             /*
  573.     printf("globalPoint in panel loop, orig = %d, %d\n",
  574.            aPanel->area.orig.x, aPanel->area.orig.y);
  575.             /* */
  576.     aPanel = aPanel->dad;
  577.     }
  578. }
  579.  
  580.     /*
  581.      *  convert a floating point in Panel's local space into viewport coords
  582.      */
  583. globalPointf(aPanel, aPoint)
  584.     register panel *aPanel;
  585.     register point2df *aPoint;
  586. {
  587.     while(aPanel) {
  588.     aPoint->x += (float)aPanel->area.orig.x;
  589.     aPoint->y += (float)aPanel->area.orig.y;
  590.             /*
  591.     printf("globalPointf in panel loop, orig = %d, %d\n",
  592.            aPanel->area.orig.x, aPanel->area.orig.y);
  593.             /* */
  594.     aPanel = aPanel->dad;
  595.     }
  596. }
  597.  
  598.     /*
  599.      *  read thePanel's current highwater mark
  600.      */
  601. getHighwater(aPanel)
  602.     register panel *aPanel;
  603. {
  604.     int highwater = 0;
  605.  
  606.     while(aPanel->dad)  {
  607.     highwater += aPanel->area.orig.y;
  608.     aPanel = aPanel->dad;
  609.     }
  610.     
  611.     return thePanel->highwater.y - highwater;
  612. }
  613.  
  614.     /*
  615.      *  set thePanel's current highwater mark
  616.      */
  617. setHighwater(newval)
  618.     int newval;
  619. {
  620.     thePanel->highwater.y = newval;
  621. }
  622.  
  623.     /*
  624.      *  increment thePanel's current highwater mark
  625.      */
  626. bumpHighwater(incr)
  627.     int incr;
  628. {
  629.     thePanel->highwater.y += incr;
  630. }
  631.